#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <unistd.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>

#define SHM_KEY_ID 0x2001
#define SHM_SIZE 	1024*1024*10

int main()
{
	int shm_fd = shmget(SHM_KEY_ID, SHM_SIZE, IPC_CREAT | 0666);
	if( -1 == shm_fd )
	{
		printf("create shm failed!\n");

		exit(1);
	}

	void *shm_ptr;
	shm_ptr = shmat(shm_fd, NULL, 0);
	if((void *)-1 == shm_ptr)
	{
		printf("shmat failed!\n");
		exit(0);
	}

	int n_value = 0;
	void *wr_ptr = shm_ptr;
	for(;;)
	{
		++n_value;
		memcpy(wr_ptr, &n_value, sizeof(int));
		int shm_free = shm_ptr + SHM_SIZE - wr_ptr;
		if( sizeof(int) == shm_free )
		{
			usleep(1);
			printf("game over! do it again.....\n");
			wr_ptr = shm_ptr;
		}
		else
			wr_ptr += sizeof(n_value);
		usleep(100);
	}

	return 0;
}


